package com.qudian.java.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.qudian.java.core.bean.EmailInfo;
import com.qudian.java.core.common.exception.BusinessException;
import com.qudian.java.core.common.utils.BaseParamValidate;
import com.qudian.java.core.constant.Constant;
import com.qudian.java.core.service.SendService;
import com.qudian.java.core.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by zhangdelong on 16/11/22.
 * 邮件发送
 */
@RestController
@RequestMapping("/mail")
public class Maill {
    @Autowired
    private SendService sendService;
    JSONObject json = new JSONObject();

    //正常发送
    @RequestMapping(value = "/send", produces = "application/json;charset=UTF-8")
    public String send(EmailInfo maill) {
        {

            Utils utils = new Utils();
            try {
                BaseParamValidate.validate(maill);
                if (!maill.getAuth().equals(Constant.getAuthKey())) {
                    json.put("code", "BIZERROR001");
                    json.put("msg", "身份验证码无效或已失效，请确认");
                    return json.toString();
                }
                System.out.println(maill.toString());
                utils.send(maill.getTitle(), maill.getContext());
            } catch (BusinessException biz) {
                json.put("code", biz.getErrorCode());
                json.put("msg", biz.getMessage());
                return json.toString();
            } catch (Exception e) {
                json.put("code", "SYS001");
                json.put("msg", "系统异常,发送失败");
                e.printStackTrace();
                return json.toString();
            }
            json.put("code", "SUCCESS");
            json.put("msg", "发送成功");
            return json.toString();
        }
    }
}
